unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, OleCtrls, SHDocVw, Menus, IniFiles,StrUtils, Vcl.ExtCtrls,Vcl.Imaging.jpeg,Unit2,Unit3,Unit4;

type
  TForm1 = class(TForm)
    Button1: TButton;
    MainMenu1: TMainMenu;
    About1: TMenuItem;
    WebBrowser3: TWebBrowser;
    Login: TButton;
    Timer1: TTimer;
    Timer2: TTimer;
    Other1: TMenuItem;
    Yahoo1: TMenuItem;
    Okazii1: TMenuItem;
    Edit3: TEdit;
    Timer3: TTimer;
    Button2: TButton;
    Button3: TButton;
    Site1: TMenuItem;
    Okazii2: TMenuItem;
    Label1: TLabel;
    Label2: TLabel;
    CheckBox1: TCheckBox;
    SLG1: TMenuItem;
    Edit1: TEdit;
    Edit2: TEdit;
    Label3: TLabel;
    Label4: TLabel;
    Edit4: TEdit;
    Label5: TLabel;
    Edit5: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure WebBrowser3DocumentComplete(ASender: TObject;
      const pDisp: IDispatch; const URL: OleVariant);
    procedure LoginClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
    procedure Yahoo1Click(Sender: TObject);
    procedure Okazii1Click(Sender: TObject);
    procedure SiteCrawler1Click(Sender: TObject);
    procedure Timer3Timer(Sender: TObject);
    procedure About1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Okazii2Click(Sender: TObject);
    procedure SLG1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
 Form1: TForm1;
 clicked: boolean;
 endofline: boolean;

ASender1: TObject;

implementation

{$R *.dfm}

function WindowsExit(RebootParam: Longword): Boolean;
var
   TTokenHd: THandle;
   TTokenPvg: TTokenPrivileges;
   cbtpPrevious: DWORD;
   rTTokenPvg: TTokenPrivileges;
   pcbtpPreviousRequired: DWORD;
   tpResult: Boolean;
const
   SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
begin
   if Win32Platform = VER_PLATFORM_WIN32_NT then
   begin
     tpResult := OpenProcessToken(GetCurrentProcess(),
       TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,
       TTokenHd) ;
     if tpResult then
     begin
       tpResult := LookupPrivilegeValue(nil,
                                        SE_SHUTDOWN_NAME,
                                        TTokenPvg.Privileges[0].Luid) ;
       TTokenPvg.PrivilegeCount := 1;
       TTokenPvg.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
       cbtpPrevious := SizeOf(rTTokenPvg) ;
       pcbtpPreviousRequired := 0;
       if tpResult then
         Windows.AdjustTokenPrivileges(TTokenHd,
                                       False,
                                       TTokenPvg,
                                       cbtpPrevious,
                                       rTTokenPvg,
                                       pcbtpPreviousRequired) ;
     end;
   end;
   Result := ExitWindowsEx(RebootParam, 0) ;
end;

function SecToTime(Sec: Integer): string;
var
   H, M, S: string;
   ZH, ZM, ZS: Integer;
begin
   ZH := Sec div 3600;
   ZM := Sec div 60 - ZH * 60;
   ZS := Sec - (ZH * 3600 + ZM * 60) ;
   H := IntToStr(ZH) ;
   M := IntToStr(ZM) ;
   S := IntToStr(ZS) ;
   Result := H + ':' + M + ':' + S;
end;

Procedure WriteToUpload(what:string);
var h,h1:hwnd;
begin
h:=FindWindow('#32770', 'Choose File to Upload');
//if h=0 then
//h:=FindWindow('#32770', 'Choose file');

h1:=FindWindowex(h,0,'ComboBoxEx32',nil);
SendMessage(h1,wm_setText,0,Integer(what));
h1:=FindWindowex(h,0,'Button', '&Open');
SendMessage(h1, BM_CLICK,0,0);
end;

procedure TForm1.About1Click(Sender: TObject);
begin
form4.Show;
end;

procedure TForm1.Button1Click(Sender: TObject);

begin
timer3.Enabled:=true;
endofline:=false;
edit1.Text:=webbrowser3.LocationURL;
timer1.Interval:=strtoint(edit2.Text);
timer2.Interval:=strtoint(edit4.Text);
timer3.Interval:=strtoint(edit5.Text);
end;



procedure TForm1.Button2Click(Sender: TObject);
begin
webbrowser3.Navigate('https://www.okazii.ro/logout/');
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
webbrowser3.Stop;
timer1.Enabled:=false;
timer2.Enabled:=false;
timer3.Enabled:=false;
edit1.Text:='';
end;



procedure TForm1.SiteCrawler1Click(Sender: TObject);
begin
form4.Show;
end;

procedure TForm1.SLG1Click(Sender: TObject);
begin
//form5.show;
webbrowser3.Navigate('http://www.servicelaptopgsm.ro/catalog/admin/categories.php');
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin

WriteToUpload( ExtractFilePath(Application.ExeName)+'images.jpg');
timer1.Enabled:=false;
timer2.Enabled:=true;
end;

procedure TForm1.Timer2Timer(Sender: TObject);
begin
if AnsiContainsStr(webbrowser3.LocationURL,'categories.php') then
begin
webbrowser3.OleObject.document.all.item('new_product',0).submit;
timer2.Enabled:=false;
end
else
try
if  webbrowser3.OleObject.document.all.item('nextphoto',0).value ='2' then
begin
webbrowser3.OleObject.document.all.item('pas3form',0).submit;
timer2.Enabled:=false;
end
else
begin
timer1.Enabled:=true;
WebBrowser3.OleObject.document.all.item('image',0).click;
end;
except
webbrowser3.Navigate('http://www.okazii.ro/vanzare/');
end;
end;

procedure TForm1.Timer3Timer(Sender: TObject);
  var S : TMemoryStream;
  Pic : TJpegImage;
begin

if endofline=false then
begin

   S := TMemoryStream.Create;
  Pic:=TJpegImage.Create;
  try
    form4.ADOTable1poza.SaveToStream(S);
    S.Position := 0;
    Pic.LoadFromStream(S);
    form4.Image2.Picture.Graphic:=Pic;
  finally
    S.Free;
  end;
   form4.Image2.Picture.SaveToFile('images.jpg');

  form4.Memo1.Text:=form4.AdoTable1descriere.Value;

 if AnsiContainsStr(webbrowser3.LocationURL,'okazii.ro') then
 webbrowser3.Navigate('http://www.okazii.ro/vanzare/');
 if AnsiContainsStr(webbrowser3.LocationURL,'servicelaptopgsm.ro') then
 webbrowser3.Navigate(edit1.Text+'&action=new_product');

 label2.Caption:=SecToTime((form4.ADOTable1.RecordCount-form4.ADOTable1.RecNo)*(timer1.Interval+timer2.Interval+timer3.Interval+2000) div 1000);
 form4.label8.Caption:=inttostr(form4.ADOTable1.RecNo);
 clicked:=false;
   end;

if form4.ADOTable1.RecNo=form4.ADOTable1.RecordCount then
begin
if (form4.listbox1.Items.Count>0) and (form4.listbox1.ItemIndex<form4.listbox1.Items.Count-1)  then
begin
form4.listbox1.Selected[form4.listbox1.ItemIndex+1]:=true;
form4.ADOTable1.LoadFromFile(form4.listbox1.Items[form4.listbox1.ItemIndex]);
form4.label5.Caption:=inttostr(form4.ADOTable1.RecordCount);
webbrowser3.Navigate('http://www.okazii.ro/vanzare/');
end
else
begin
endofline:=true;
if checkbox1.Checked=true then
WindowsExit(EWX_POWEROFF or EWX_FORCE);
end;
end;
timer3.Enabled:=false;
end;

procedure TForm1.Okazii1Click(Sender: TObject);
begin
form3.Show;
end;


procedure TForm1.Okazii2Click(Sender: TObject);
begin
webbrowser3.Navigate('http://www.okazii.ro/');
end;

procedure TForm1.LoginClick(Sender: TObject);
begin
webbrowser3.Navigate('https://www.okazii.ro/login/');
end;


procedure TForm1.WebBrowser3DocumentComplete(ASender: TObject;
  const pDisp: IDispatch; const URL: OleVariant);
var i: integer;
begin

//if webbrowser3.LocationURL  = 'https://www.okazii.ro/login/' then
//begin
//(ASender as TWebBrowser).OleObject.document.all.item('username',0).value:=edit3.Text;
//(ASender as TWebBrowser).OleObject.document.all.item('password',0).value:='1234asdf';
//(ASender as TWebBrowser).OleObject.document.all.item('loginbtn',0).click;

//end;

if webbrowser3.LocationURL  = 'http://www.okazii.ro/vanzare/' then
try
if(ASender as TWebBrowser).OleObject.document.all.item('keyword',0).value<>form4.ADOTable1['titlu'] then
begin
(ASender as TWebBrowser).OleObject.document.all.item('keyword',0).value:=string(form4.ADOTable1['titlu']);
(ASender as TWebBrowser).OleObject.document.all.item('sugestion',0).click;
end;
except
webbrowser3.Navigate('http://www.okazii.ro/vanzare/');
end;

if AnsiContainsStr(webbrowser3.LocationURL,'pas-2') then
try
if (ASender as TWebBrowser).OleObject.document.all.item('keyword',0).value=form4.ADOTable1['titlu'] then
begin
if form4.RadioButton1.Checked=true then
(ASender as TWebBrowser).OleObject.document.all.item('recommended',1).click;
if form4.RadioButton3.Checked=true then
(ASender as TWebBrowser).OleObject.document.all.item('catalogid',0).value:='8254';
if form4.RadioButton4.Checked=true then
(ASender as TWebBrowser).OleObject.document.all.item('catalogid',0).value:='15777';
if form4.RadioButton5.Checked=true then
(ASender as TWebBrowser).OleObject.document.all.item('catalogid',0).value:='8611';
if form4.RadioButton6.Checked=true then
(ASender as TWebBrowser).OleObject.document.all.item('catalogid',0).value:='8292';
if form4.RadioButton7.Checked=true then
(ASender as TWebBrowser).OleObject.document.all.item('catalogid',0).value:='5750';

if form4.RadioButton2.Checked=false then
(ASender as TWebBrowser).OleObject.document.all.item('action',0).click;

end

else
 begin
(ASender as TWebBrowser).OleObject.document.all.item('keyword',0).value:=string(form4.ADOTable1['titlu']);
(ASender as TWebBrowser).OleObject.document.all.item('sugestion',0).click;

end;
except
webbrowser3.Navigate('http://www.okazii.ro/vanzare/');
end;

if AnsiContainsStr(webbrowser3.LocationURL,'pas-3') then
begin
 try
if WebBrowser3.OleObject.document.all.item('error').innertext<>'' then
webbrowser3.Navigate('http://www.okazii.ro/vanzare/');
except

end;
if WebBrowser3.OleObject.document.all.item('titlet',0).value<>form4.ADOTable1['titlu']  then
begin
 try
WebBrowser3.OleObject.document.all.item('titlet',0).value:=string(form4.ADOTable1['titlu']);
WebBrowser3.OleObject.document.all.item('title',0).value:=string(form4.ADOTable1['titlu']);
WebBrowser3.OleObject.document.all.item('state',0).value:=1;
WebBrowser3.OleObject.document.all.item('invoice',0).value:=2;
WebBrowser3.OleObject.document.all.item('warranty',0).value:=2;
WebBrowser3.OleObject.document.all.item('description',0).value:=string(form4.ADOTable1['descriere']);
WebBrowser3.OleObject.document.all.item('pret_pornire_3',0).value:=integer(form4.ADOTable1['pret']);
WebBrowser3.OleObject.document.all.item('quantity_3',0).value:=100;
WebBrowser3.OleObject.document.all.item('auction_period',0).value:=30;
WebBrowser3.OleObject.document.all.item('repost',0).value:=2;
WebBrowser3.OleObject.document.all.item('payment_personal',0).click;
WebBrowser3.OleObject.document.all.item('delivery_time',0).value:=1;
WebBrowser3.OleObject.document.all.item('forum',1).click;


timer1.Enabled:=true;
WebBrowser3.OleObject.document.all.item('image',0).click;
except
webbrowser3.Navigate('http://www.okazii.ro/vanzare/');
 end;
end;


end;

if AnsiContainsStr(webbrowser3.LocationURL,'pas-4') then
begin
try
(ASender as TWebBrowser).OleObject.document.all.item('btnContinuaVanzarea',0).click;

except
webbrowser3.Navigate('http://www.okazii.ro/vanzare/');
end;
end;


if AnsiContainsStr(webbrowser3.LocationURL,'pas-5') then
begin
 try
(ASender as TWebBrowser).OleObject.document.all.item('buton_final',0).click;
except
webbrowser3.Navigate('http://www.okazii.ro/vanzare/');
end;
end;

if (AnsiContainsStr(webbrowser3.LocationURL,'finalizare-vanzare')) or (AnsiContainsStr(webbrowser3.LocationURL,'pas-6'))then
if clicked=false then
begin
clicked:=true;
form4.ADOTable1.Next;
timer3.Enabled:=true;
end;

if AnsiContainsStr(webbrowser3.LocationURL,'servicelaptopgsm.ro') then
begin

if AnsiContainsStr(webbrowser3.LocationURL,'action=new_product') then
begin
WebBrowser3.OleObject.document.all.item('products_name[2]',0).value:=string(form4.ADOTable1['titlu']);
WebBrowser3.OleObject.document.all.item('products_price',0).value:=string(form4.ADOTable1['pret']);
timer1.Enabled:=true;
WebBrowser3.OleObject.document.all.item('products_image',0).click;
end;

if (AnsiContainsStr(webbrowser3.LocationURL,edit1.Text)) and (AnsiContainsStr(webbrowser3.LocationURL,'action=new_product')=false)then
begin
clicked:=true;
form4.ADOTable1.Next;
timer3.Enabled:=true;
end;

end;

end;

procedure TForm1.Yahoo1Click(Sender: TObject);
begin
form2.Show;
end;


 end.
